package com.yl.chainStore.payrule.item.service

import com.yl.chainStore.entity.pay.PayCustomerOrder
import com.yl.chainStore.entity.pay.PayCustomerOrderItemDiscountRuleLog
import com.yl.chainStore.payrule.common.DiscountComputeOrderResultVo
import com.yl.chainStore.payrule.item.ItemRuleService
import com.yl.chainStore.payrule.item.ItemRuleTypeEnum
import com.yl.chainStore.payrule.item.entity.MoneyToMoneyTicketRule
import com.yl.chainStore.services.IWebCompanyDeptService
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class MoneyToMoneyTicketRuleServiceImpl: ItemRuleService() {

	@Autowired
	private lateinit var iWebCompanyDeptService: IWebCompanyDeptService

	private val ruleType = ItemRuleTypeEnum.MONEY_TO_MONEY_TICKET_RULE


	override fun computeOrder(order: PayCustomerOrder): DiscountComputeOrderResultVo {
		val ruleList =
			iWebCompanyDeptService.getNowOpenItemDiscountRuleList(getContext().subCompanyId, ruleType)
		for(itemRule in ruleList){
			val rule = itemRule.getOtherContentObj() as MoneyToMoneyTicketRule
			for(detail in order.details!!){
				if(detail.gift == 1){
					continue
				}
				if(detail.itemId == rule.itemId){
					//for 代表可以多张用 不用break也是
					for(i in 1..detail.number){
						val useForOrderDetails = rule.getUseForOrderDetails(order)
						val discountPartDetail = discountPartDetail(useForOrderDetails, rule.disCountMoney)
						order.originalPrice = order.originalPrice - discountPartDetail
						order.addPayCustomerOrderItemDiscountRuleLog(
							PayCustomerOrderItemDiscountRuleLog(
								id = 0L,
								orderId = order.id,
								detailId = detail.id,
								content = itemRule.title,
								ruleType = ruleType,
								ruleId = itemRule.id,
								discountMoney = discountPartDetail
							)
						)
					}
				}
			}
		}

		return DiscountComputeOrderResultVo(returnSuccess(),order)
	}
}




